#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;
using db = double;
using tl = tuple < int, int, int >;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 310;

int n, a[N], c[N];

unordered_map < int, ll > dis;
priority_queue < pii, vector < pii >, greater < pii > > q;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) a[i] = in;
	rep(i, 1, n) c[i] = in;
	q.ep(0, 0); dis[0] = 0;
	while(q.size()) {
		int x = q.top().se; ll d = q.top().fi; q.pop();
		if(d != dis[x]) continue;
		rep(i, 1, n)
			if(__gcd(a[i], x) != x) {
				int nx = __gcd(a[i], x);
				if(!dis.count(nx) || dis[nx] > d + c[i]) {
					dis[nx] = d + c[i]; q.ep(dis[nx], nx);
				}
			}
	}
	if(!dis.count(1)) puts("-1");
	else printf("%lld\n", dis[1]);
	return 0;
}
